<?php

namespace haibao\cms\data\activity;

use haibao\cms\model\filter\activity\Activity as ActivityFilter;

class SpiderData extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\activity\SpiderImage');
    }

    public function getSpiderImageCntFromTmall(){
        $sql = 'SELECT count(*) as cnt,SiteInfo,`Status` FROM `SpiderImage`  GROUP BY SiteInfo, `Status` ;';
        $result = $this->query($sql);

        $list = array();

        while($result->fetch_assoc()){

            if (!isset($list[$result->getData('SiteInfo')])) $list[$result->getData('SiteInfo')] = array('new'=>0,'del'=>0,'confirm'=>0);

            $status = $result->getData('Status');
            if($status == \haibao\cms\model\data\activity\SpiderImage::STATUS_ADD){
                $list[$result->getData('SiteInfo')]['new'] = $result->getData('cnt');
            }elseif($status == \haibao\cms\model\data\activity\SpiderImage::STATUS_DEL){
                $list[$result->getData('SiteInfo')]['del'] = $result->getData('cnt');
            }elseif($status == \haibao\cms\model\data\activity\SpiderImage::STATUS_CONFIRM){
                $list[$result->getData('SiteInfo')]['confirm'] = $result->getData('cnt');
            }
        }
        return $list;
    }

    public function delSpiderImage($id){
        return $this->query("update SpiderImage set Status=-1 where Id=%s", array($id));
    }

    public function confirmSpiderImage($ids, $secordCategory){
        $str = preg_replace('(\d+)','%s',implode(',', $ids));
        if(in_array($secordCategory, array(6, 7, 8))){
            $sql = "update SpiderImage set Status=1,SecondCategory2=".$secordCategory." where Id in (".$str.")";
        }else{
            $sql = "update SpiderImage set Status=1,SecondCategory=".$secordCategory." where Id in (".$str.")";
        }

        return $this->query($sql, $ids);
    }

    public function getFinishPictureCount($type, $secordCategory){
        if($type == 50025263){
            $type = '50025263, 50102772';
        }elseif($type == '52040006'){
            $type = '52040006, 54652002, 50047357';
        }
        $this->setReadPerformance(true);
        if(in_array($secordCategory, array(6, 7, 8))){
            $sql = 'select count(*) as count from SpiderImage where SiteInfo in('.$type.') and SecondCategory2=%s and Status!=-1';
        }else{
            $sql = 'select count(*) as count from SpiderImage where SiteInfo in('.$type.') and SecondCategory=%s and Status!=-1';
        }
        $result = $this->query($sql, array(
            $secordCategory
        ))->fetch_assoc();

        return $result['count'];
    }
}